<?php 
session_start();
include "../_init_.php";


$dtRent = new DataTable(constant('DB_TABLE_RENT'));
//增加数据
$action= isset($_REQUEST["action"])?$_REQUEST["action"]:'';
switch($action)
{
	case 'delete':
		//delete image
		
		$current_data = $dtRent->get($_REQUEST['id']);
		$dtRentImage = new DataTable(constant('DB_TABLE_RENT_IMAGE'));
		$condition = sprintf("`rId`=%d",$current_data['rId']);
		$img_list = $dtRentImage->get_list($condition);
		foreach($img_list as $tmpData)
		{
			$img_path = constant('SITE_WEB_DISK_DIR').$tmpData['image'];
			unlink($img_path);
			$dtRentImage->remove($tmpData['id']);
		}
		$img_path = constant('SITE_WEB_DISK_DIR').$current_data['image'];
		unlink($img_path);
		$dtRent->remove($_REQUEST['id']);
	break;
}
$DT = new DataTable(constant('DB_TABLE_RENT'));
$condition = "1 = 1 ";
if(isset($_REQUEST['sText']) && strlen($_REQUEST['sText']) > 0)
{
	$condition .= ' AND (';
	$condition .= sprintf("`id` = %d",mysql_real_escape_string($_REQUEST['sText']));
	$condition .= sprintf("OR `title` like '%%%s%%'",mysql_real_escape_string($_REQUEST['sText']));
	$condition .= sprintf(" OR `address` like '%%%s%%'",mysql_real_escape_string($_REQUEST['sText']));
	$condition .= ')';
}
$totalCount = $DT->get_count($condition);

$pagesize = isset($_REQUEST['pagesize']) && intval($_REQUEST['pagesize']) > 0?$_REQUEST['pagesize']:10;
$pagenumber = isset($_REQUEST['pagenumber']) && intval($_REQUEST['pagenumber']) > 0?$_REQUEST['pagenumber']:1;
$pagecount = ceil($totalCount/$pagesize);

$offset = ($pagenumber - 1)*$pagesize;
$orderby = array('sn');
$data_list = $DT->get_list($condition,$orderby,$pagesize,$offset);
?>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網站管理系統</title>


<LINK REL="stylesheet" HREF="css/style.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="css/main.css" TYPE="text/css">
<script src="js/FancyZoom.js" language="JavaScript" type="text/javascript"></script>
<script src="js/FancyZoomHTML.js" language="JavaScript" type="text/javascript"></script>

<script src="../js/jquery-ui-1.7.3.custom/js/jquery-1.3.2.min.js" language="JavaScript" type="text/javascript"></script>
<script src="../js/jquery.pager.js"></script>
<link href="../css/page.css" rel="stylesheet" type="text/css" /> 
<script language="javascript">
$(function(){
	$(".pager").pager({ pagenumber: <?php echo $pagenumber;?>, pagecount: <?php echo $pagecount;?>, buttonClickCallback: function(pageclickednumber){
			<?php if(isset($_REQUEST['sText']) && strlen($_REQUEST['sText']) > 0):?>
			document.location='?sText=<?php echo urlencode($_REQUEST['sText']);?>&pagenumber='+pageclickednumber;
			<?php else:?>
			document.location='?pagenumber='+pageclickednumber;
			<?php endif;?>
			
     } }); 
});
function ConfirmDel()
{
   if(confirm("確定要刪除選擇的項目嗎？一旦刪除將不能恢複！"))
     return true;
   else
     return false;	 
}
</script>
<body onLoad="setupZoom();">
<table width="98%" border="0" class="table2">
  <tr>
    <td>房源管理 - 房源</td>
  </tr>
</table>
<?php if($error_message):?>
<div class="error_message"><?php echo $error_message;?></div>
<?php endif;?>
<?php if($sucess_message):?>
<div class="success_message"><?php echo $sucess_message;?></div>
<?php endif;?>
 <table width="820" border="0" cellpadding="2" cellspacing="1" class="table_southidc">
 		<tr>
 			<td>
 				<input type="text" name="sText" value="<?php echo $_REQUEST['sText'];?>"/>&nbsp;<input type="button" value="查詢" onclick="document.location='?sText='+$('input[name=sText]').val();"/>
 			</td>
 		</tr>
        <tr> 
          <td  height="20" class="back_southidc"> <div align="center"><strong>房源管理</strong></div></td>
        </tr>
        <tr  class="tr_southidc">          
            <td bgcolor="#B0DAFF">
              <table width="100%" border="0" align="center" cellpadding="0" cellspacing="2" bordercolor="#0000FF">
                <tr bgcolor="#E4F2FF"> 
                  <td width="10%" height="25"  bgcolor="#D9EDFF"> 
                  	<div align="center">標題</div>
                  </td>
                  <td width="10%" bgcolor="#D9EDFF"> 
                  	<div align="center">類型</div>
                  </td>
                  <td width="10%" bgcolor="#D9EDFF"> 
                  	<div align="center">坪數</div>
                  </td>
                  <td width="10%" bgcolor="#D9EDFF"> 
                  	<div align="center">租金</div>
                  </td>
                	<td width="10%" bgcolor="#D9EDFF"> 
                  	<div align="center">圖片</div>
                  </td>
                  <td width="8%" bgcolor="#D9EDFF"> 
                  	<div align="center">操作</div>
                  </td>
                  <td width="5%" bgcolor="#D9EDFF"> 
                  	<div align="center">刪除</div>
                  </td>
                </tr>
<?php 
	
	$dtRentType = new DataTable(constant('DB_TABLE_RENT_TYPE'));
      $renttype_list = $dtRentType->get_list('','sn');
     $renttype_table = array();
      foreach($renttype_list  as $tmprow)
      {
      	$renttype_table[$tmprow['id']] = $tmprow['name'];
      }
	foreach($data_list as $row):
	
	$type_id_list = explode(',',$row['type']);
	$type_name_str = '';
      	if(is_array($type_id_list))
      	{
      		foreach($type_id_list as $type_id)
      		{
      			if(intval($type_id) > 0)
      			{
      				$type_name_str .= $renttype_table[$type_id].', ';
      			}
      		
      		}
      	}
      	
      	if(strlen($type_name_str) > 0)
      		$type_name_str = substr($type_name_str,0,-2);
	?>
 <tr bgcolor="#FFFFFF" onMouseOver="this.style.backgroundColor='#E4F2FF'" onMouseOut="this.style.backgroundColor=''"> 
                  <td height="22"> 
                  <div align="center"><?php echo $row['title'];?></div></td>
                  <td> 
                  <div align="center"><?php echo $type_name_str;?></div>
                  </td>
                     <td> 
                  <div align="center"><?php echo $row['size'];?></div>
                  </td>
                	<td> 
                  <div align="center"><?php echo $row['cost'];?></div>
                  </td>
                  	<td> 
                  <div align="center"><image src="<?php echo constant('SITE_URL_PATH').$row['image'];?>" width="60" height="60"/></div>
                  </td>
                  <td> 
                    <div align="center">
                  <a href='rent_info1.php?action=edit&id=<?php echo $row['id'];?>'>修改</a>                    </div></td>
                  <td> 
                    <div align="center">
				  <a href="?id=<?php echo $row['id'];?>&action=delete" onClick="return ConfirmDel();">刪除</a>                    </div></td>
                </tr>
<?php endforeach;?>
          </table></td>         
        </tr>
      </table>
      <div class="pager"></div>
</body>
</html>